//
//  SearchViewController.m
//  TextApplication
//
//  Created by dllo on 16/9/19.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "SearchViewController.h"
#import "TAMacro.h"
#import "SearchCollectionViewCell.h"
#import "ZWYNetTool.h"
#import "MasterDetailViewController.h"
#import "TextView.h"

#import "MBProgressHUD.h"
#import "MJRefresh.h"

#import "MasterFirst.h"
#import "MasterData.h"
#import "MasterItems.h"
#import "MasterImage.h"


@interface SearchViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) MasterFirst *first;
@property (nonatomic, strong) NSMutableArray <MasterItems *>*dataArray;
@property (nonatomic, assign) NSInteger page;
@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.page = 1;
    self.title = @"搜索结果";
    [self createLeft];
    [self createCollectionView];
    [self createSearchData];

}

- (void)createLeft {
    UIButton *left = [TextView createButtonWithImage:[UIImage imageNamed:@"10"]];
    [left addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:left];
}

- (void)backClick:(UIButton *)button {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)createSearchData {
  
    NSString *str1 = @"http://mobile.iliangcang.com/user/search?&page=";
    NSString *str3 = @"&count=10&keyword=";
    NSString *str2 = @"&app_key=iPhone&v=3.0.0&sig=254F2CCC-7E43-4833-A27D-7D12702F5981&user_token=03723b5fa4100c1079e788f94d65908d";
    NSString *str = [[[[str1 stringByAppendingString: [NSString stringWithFormat:@"%ld", self.page]] stringByAppendingString: str3] stringByAppendingString: self.textFieldStr] stringByAppendingString:str2];
    NSDictionary *headDic = @{@"Host": @"mobile.iliangcang.com", @"Proxy-Connection": @"keep-alive", @"Accept": @"*/*", @"Accept-Language": @"zh-Hans-CN;q=1, en-CN;q=0.9", @"Connection": @"keep-alive", @"User-Agent": @"iLiangCang/3.0.0 (iPhone; iOS 9.2.1; Scale/2.00) Paros/3.2.13"};
    [ZWYNetTool GET:str andBody:nil andHeader:headDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        if (self.page == 1) {
            self.dataArray = [NSMutableArray array];
        }
        self.first = [MasterFirst modelWithDic:result];
        for (MasterItems *item in self.first.data.items) {
            [self.dataArray addObject:item];
        }
        self.page = self.page + 1;
        [self.collectionView reloadData];
    } andFailureBlock:^(NSError *error) {
    }];
}


- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((WIDTH - 40) / 3, (WIDTH - 40) / 3 + 30);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 44) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[SearchCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

// 下拉刷新
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (self.collectionView.contentOffset.y < 0) {
        self.page = 1;
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.label.text = NSLocalizedString(@"loading", @"HUD loading title");
        NSLog(@"下拉刷新");
        [hud hideAnimated:YES afterDelay:1];
        [self createSearchData];
//        [self.collectionView reloadData];
    }
}

// 上拉加载
- (void)upLoad {
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self createSearchData];
        [self.collectionView.mj_footer endRefreshing];
    }];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    [self upLoad];
    SearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.item = self.dataArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    MasterDetailViewController *detailVC = [[MasterDetailViewController alloc] init];
    detailVC.netID = self.dataArray[indexPath.row].uid;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
